package doctor

import (
	"context"
	"fmt"
	"github.com/gin-gonic/gin"
	"google.golang.org/grpc"
	"net/http"
	__ "srv/proto"
)

type Find struct {
	Name string `form:"name" json:"name" xml:"name"`
}

func FindHao(c *gin.Context) {
	var json Find
	if err := c.ShouldBindJSON(&json); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	// 1.连接
	conn, err := grpc.Dial("127.0.0.1:8888", grpc.WithInsecure())
	if err != nil {
		fmt.Printf("连接异常： %s\n", err)
	}
	defer conn.Close()
	// 2. 实例化gRPC客户端
	client := __.NewDoctorServiceClient(conn)
	// 3.组装请求参数
	req := new(__.FindHaoRequest)
	req.Name = json.Name
	// 4. 调用接口
	response, err := client.FindHao(context.Background(), req)
	if err != nil {
		fmt.Println("响应异常  %s\n", err)
	}
	fmt.Printf("响应结果： %v", response)
	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "展示成功",
		"data":    response,
	})
}
